﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Job.Core
{
    /// <summary>
    /// 日期操作类
    /// </summary>
    public class DateHelper
    {
        /// <summary>
        /// 将日期转换为英文格式 e.g. Nov 01, 2010
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string ConvertToEnDate(DateTime dt)
        {
            return dt.ToString("MMM dd, yyyy", new CultureInfo("en-us", false));
        }

        /// <summary>
        /// 将日期转换为类似facebook日期格式 e.g.(1 seconds ago/1 minutes ago/1 hours ago/1 days ago/1 month ago/may 1 at 5:32pm/may 1, 2009 at 5:32pm)
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string ConvertToFaceBookDate(DateTime dt)
        {
            DateTime dtNow = DateTime.Now;
            int intSeconds = (int)dtNow.Subtract(dt).TotalSeconds;
            int intMinutes = (int)dtNow.Subtract(dt).TotalMinutes;
            int intHours = (int)dtNow.Subtract(dt).TotalHours;
            int intDays = (int)dtNow.Subtract(dt).TotalDays;
            int intMonths = intDays % 30 == 0 ? intDays / 30 : (intDays / 30 + 1);

            if (intSeconds < 60)//不足一分钟
                return string.Format("{0} seconds ago", intSeconds);
            else if (intMinutes < 60)//不足一小时
                return string.Format("{0} minutes ago", intMinutes);
            else if (intHours < 24)//不足一天
                return string.Format("{0} hours ago", intHours);
            else if (intDays < 30)//不足1月
                return string.Format("{0} days ago", intDays);
            else if (intMonths < 3)//不足一个季度
                return string.Format("{0} months ago", intMonths);
            else if (dt.Year == dtNow.Year)//未跨年
                return string.Format("{0} at {1}", dt.ToString("MMMM dd", new CultureInfo("en-us", false)), dt.ToString("h:mmtt", new CultureInfo("en-us", false)).ToLower());
            else
                return string.Format("{0} at {1}", dt.ToString("MMMM dd, yyyy ", new CultureInfo("en-us", false)), dt.ToString("h:mmtt", new CultureInfo("en-us", false)).ToLower());
        }

        /// <summary>
        /// 时间区间格式转换 e.g.(May 27 · 8:30pm - 11:30pm/May 27, 2009 · 8:30pm - 11:30pm/May 27 · 8:30pm - May 28 · 11:30pm/May 27, 2009 · 8:30pm - May 28, 2009 · 11:30pm )
        /// </summary>
        /// <param name="dtStart"></param>
        /// <param name="dtEnd"></param>
        /// <returns></returns>
        public static string ConvertToFaceBookEventDate(DateTime dtStart, DateTime dtEnd)
        {
            if (dtStart.Day == dtEnd.Day)
                return string.Format("{0} · {1} - {2}", dtStart.Year != DateTime.Now.Year ? dtStart.ToString("MMMM dd, yyyy", new CultureInfo("en-us", false)) : dtStart.ToString("MMMM dd", new CultureInfo("en-us", false)), dtStart.ToString("h:mmtt", new CultureInfo("en-us", false)).ToLower(), dtEnd.ToString("h:mmtt", new CultureInfo("en-us", false)).ToLower());
            else
                return string.Format("{0} at {1} - {2} at {3}", dtStart.Year != DateTime.Now.Year ? dtStart.ToString("MMMM dd, yyyy", new CultureInfo("en-us", false)) : dtStart.ToString("MMMM dd", new CultureInfo("en-us", false)), dtStart.ToString("h:mmtt", new CultureInfo("en-us", false)).ToLower(), dtEnd.Year != DateTime.Now.Year ? dtEnd.ToString("MMMM dd, yyyy", new CultureInfo("en-us", false)) : dtEnd.ToString("MMMM dd", new CultureInfo("en-us", false)), dtEnd.ToString("h:mmtt", new CultureInfo("en-us", false)).ToLower());
        }
    }
}
